using HKB.Common;
using System;
using System.Data;
using System.Web;
namespace HKB.Web.Shop
{
	public class OnlineUser : HoWave_Root
	{
		private new Conn conn = null;
		public int UserID = 0;
		public string UserName;
		public string Password;
		public string GroupName;
		public int GroupID;
		public int GoldCoin;
		public int UserEP;
		public decimal Balance;
		public int IsLocked;
		public int UnreadMsg;
		public string UserEmail;
		public string PassportID;
		public int ShopID;
		public string LastLoginIP;
		public DateTime LastLoginTime;
		public string GroupPower;
		public string SaleCrediet;
		public string BuyCrediet;
		public int IdVerifiy;
		public int CompanyVerifiy;
		public int BandShopVerifiy;
		public int SecurityVerifiy;
		public string ErrMsg = "";
		private string cookiesName = "";
		private string domainArea = "";
		private AppSetings apps = new AppSetings();
		private DataRow _userres = null;
		private DataRow _userinfor = null;
		public string DomainArea
		{
			get
			{
				this.domainArea = this.apps.GetConfig("DomainArea").Trim();
				return this.domainArea;
			}
			set
			{
				this.domainArea = value;
			}
		}
		public string CookiesName
		{
			get
			{
				this.cookiesName = this.apps.GetConfig("CookiesName").Trim();
				return this.cookiesName;
			}
			set
			{
				this.cookiesName = value;
			}
		}
		public DataRow UserRes
		{
			get
			{
				return this._userres;
			}
		}
		public DataRow UserInforRes
		{
			get
			{
				if (this._userinfor == null)
				{
					this.GetUserInfor();
				}
				return this._userinfor;
			}
		}
		public OnlineUser()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public OnlineUser(Conn _conn)
		{
			this.conn = _conn;
		}
		public bool UserLogin()
		{
			bool result;
			if (this.CreateLogin())
			{
				this.CreateCookies();
				this.conn.Execute("Update [HL_User] Set LastLoginIP=LoginIP, LastLoginTime=LoginTime Where UserID=" + this.UserID);
				this.conn.Execute(string.Concat(new object[]
				{
					"Update [HL_User] Set LoginIP='",
					Fetch.UserIp,
					"', LoginTime='",
					DateTime.Now,
					"', LoginTimes=LoginTimes+1 Where UserID=",
					this.UserID
				}));
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}
		public void UserLoginAddScope(int userExp)
		{
			this.conn.Execute(string.Concat(new object[]
			{
				"Update [HL_User] Set UserEP=UserEP+",
				userExp,
				" WHERE UserID=",
				this.UserID
			}));
			this.UserEP += userExp;
		}
		public bool UserLogin(string _uname, string pwd)
		{
			this.UserName = _uname;
			this.Password = Text.Md5(pwd, 32);
			bool result;
			if (this.CreateLogin())
			{
				this.CreateCookies();
				this.conn.Execute("Update [HL_User] Set LastLoginIP=LoginIP, LastLoginTime=LoginTime Where UserID=" + this.UserID);
				this.conn.Execute(string.Concat(new object[]
				{
					"Update [HL_User] Set LoginIP='",
					Fetch.UserIp,
					"', LoginTime='",
					DateTime.Now,
					"', LoginTimes=LoginTimes+1 Where UserID=",
					this.UserID
				}));
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}
		public bool CheckUserLogin()
		{
			bool result;
			if (HttpContext.Current.Request.Cookies[this.CookiesName] == null)
			{
				result = false;
			}
			else
			{
				string text = HttpContext.Current.Server.UrlDecode(Fetch.GetCookies("Howave_UserName"));
				string text2 = HttpContext.Current.Server.UrlDecode(Fetch.GetCookies("Howave_UserPassword"));
				if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2))
				{
					this.UserName = text;
					this.Password = text2;
					result = this.CreateLogin();
				}
				else
				{
					result = false;
				}
			}
			return result;
		}
		public bool CreateLogin()
		{
			bool result;
			if (!string.IsNullOrEmpty(this.UserName) && !string.IsNullOrEmpty(this.Password))
			{
				UserBll userBll = new UserBll(this.conn);
				DataTable userByUserName = userBll.GetUserByUserName(this.UserName);
				if (userByUserName.Rows.Count <= 0)
				{
					this.ErrMsg = Language.Get("PassPort/NoUserName");
					result = false;
				}
				else
				{
					this._userres = userByUserName.Rows[0];
					string str = this._userres["Password"].ToString().ToLower();
					this.Password = this.Password.ToLower();
					if (!Text.CheckMd5Text(str, this.Password))
					{
						this.ErrMsg = Language.Get("PassPort/PasswordErr");
						this.Logout();
						result = false;
					}
					else
					{
						if (this._userres["IsLocked"].ToString() == "1")
						{
							this.ErrMsg = Language.Get("PassPort/IsLocked");
							this.Logout();
							result = false;
						}
						else
						{
							this.UserID = userByUserName.Rows[0]["UserID"].ToInt32(new int[0]);
							this.UserName = this._userres["UserName"].IsNull(new object[0]);
							this.Password = this._userres["Password"].IsNull(new object[0]);
							this.GroupName = this._userres["GroupName"].IsNull(new object[0]);
							this.GroupID = this._userres["GroupID"].ToInt32(new int[0]);
							this.UserEmail = this._userres["Email"].IsNull(new object[0]);
							this.IsLocked = this._userres["IsLocked"].ToInt32(new int[0]);
							this.UnreadMsg = this._userres["UnreadMsg"].ToInt32(new int[0]);
							this.ShopID = this._userres["ShopID"].ToInt32(new int[0]);
							this.Balance = this._userres["Balance"].ToDecimal2();
							this.UserEP = this._userres["UserEP"].ToInt32(new int[0]);
							this.LastLoginIP = this._userres["LastLoginIP"].IsNull(new object[0]);
							if (this._userres["LastLoginTime"] != DBNull.Value && this._userres["LastLoginTime"] != null)
							{
								this.LastLoginTime = Convert.ToDateTime(this._userres["LastLoginTime"].ToString());
							}
							else
							{
								this.LastLoginTime = DateTime.Now;
							}
							result = true;
						}
					}
				}
			}
			else
			{
				result = false;
			}
			return result;
		}
		public void GetUserInfor()
		{
			UserInfoBll userInfoBll = new UserInfoBll(this.conn);
			DataTable userInfoByUserID = userInfoBll.GetUserInfoByUserID(this.UserID);
			if (userInfoByUserID.Rows.Count > 0)
			{
				this._userinfor = userInfoByUserID.Rows[0];
				this.IdVerifiy = Text.ToInt32(this._userinfor["AuthenticationType"].ToString());
				this.CompanyVerifiy = Text.ToInt32(this._userinfor["CompanyIdentify"].ToString());
				this.BandShopVerifiy = Text.ToInt32(this._userinfor["BandShopIdentify"].ToString());
				this.SecurityVerifiy = Text.ToInt32(this._userinfor["OtherIdentify2"].ToString());
				this.SaleCrediet = this._userinfor["SaleCrediet"].ToString();
				this.BuyCrediet = this._userinfor["BuyCrediet"].ToString();
				this.SaleCrediet = ((this.SaleCrediet == "") ? "0" : this.SaleCrediet);
				this.BuyCrediet = ((this.BuyCrediet == "") ? "0" : this.BuyCrediet);
			}
			else
			{
				this.Logout();
			}
		}
		public bool CheckGroupPower(string key)
		{
			GroupBll groupBll = new GroupBll(this.conn);
			if (this.GroupPower == null)
			{
				DataTable groupPowerByID = groupBll.GetGroupPowerByID(this.GroupID);
				if (groupPowerByID.Rows.Count > 0)
				{
					this.GroupPower = groupPowerByID.Rows[0]["GroupPower"].ToString();
				}
				else
				{
					this.GroupPower = "";
				}
			}
			return groupBll.CheckGroupPower(this.GroupPower, key);
		}
		public string GetGroupPower(string key)
		{
			GroupBll groupBll = new GroupBll(this.conn);
			DataTable groupByGroupID = groupBll.GetGroupByGroupID(this.GroupID);
			string result;
			if (groupByGroupID.Rows.Count > 0)
			{
				result = groupByGroupID.Rows[0][key].ToString();
			}
			else
			{
				result = "";
			}
			return result;
		}
		public void Logout()
		{
			HttpCookie httpCookie = new HttpCookie(this.CookiesName);
			httpCookie.Domain = this.DomainArea;
			httpCookie.Expires = DateTime.Now.AddDays(-1.0);
			HttpContext.Current.Response.Cookies.Add(httpCookie);
		}
		public void CreateCookies()
		{
			HttpCookie httpCookie = new HttpCookie(this.CookiesName);
			httpCookie.Domain = this.DomainArea;
			httpCookie["Howave_UserID"] = this.UserID.ToString();
			httpCookie["Howave_UserName"] = HttpContext.Current.Server.UrlEncode(this.UserName);
			httpCookie["Howave_UserPassword"] = HttpContext.Current.Server.UrlEncode(this.Password);
			httpCookie.Expires = DateTime.Now.AddMonths(1);
			HttpContext.Current.Response.Cookies.Add(httpCookie);
		}
	}
}
